package com.wjs.gulimall.gulimallmember.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.common.exception.BizCodeEnum;
import com.wjs.gulimall.gulimallmember.exception.PhoneExsitException;
import com.wjs.gulimall.gulimallmember.exception.UsernameExsitException;
import com.wjs.gulimall.gulimallmember.feign.CouponFeign;
import com.wjs.gulimall.gulimallmember.vo.MemberLoginVo;
import com.wjs.gulimall.gulimallmember.vo.MemberRegistVo;
import com.wjs.gulimall.gulimallmember.vo.SocialUserVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wjs.gulimall.gulimallmember.entity.MemberEntity;
import com.wjs.gulimall.gulimallmember.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 会员
 *
 * @author wangjingsong
 * @email wangjingsong@gmail.com
 * @date 2022-04-17 15:26:57
 */
@RestController
@RequestMapping("gulimallmember/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeign couponFeign;

    @GetMapping("/test")
    public R test(){
        R test = couponFeign.coupontest();
        return R.ok().put("member","我是member").put("查到",test.get("coupon"));
    }
    @GetMapping("/tests")
    public R tests(){
        return R.ok().put("我是","新建");
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }

    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo vo){
        try {
            memberService.regist(vo);
        } catch (PhoneExsitException e) {
           return R.error(BizCodeEnum.PHONE_EXEIT_EXCEPTION.getCode(),BizCodeEnum.PHONE_EXEIT_EXCEPTION.getMessgae());
        }catch (UsernameExsitException e){
            return R.error(BizCodeEnum.USERNAME_EXEIT_EXCEPTION.getCode(), BizCodeEnum.USERNAME_EXEIT_EXCEPTION.getMessgae());
        }
        return R.ok();
    }
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo){
        MemberEntity entity=memberService.login(vo);
        if(entity!=null){
            return R.ok().setData(entity);
        }else {
            return R.error(BizCodeEnum.PHONE_PASSWORD_EXCEPTION.getCode(), BizCodeEnum.PHONE_PASSWORD_EXCEPTION.getMessgae());
        }
    }


    @PostMapping("/oauth/login")
    public R oauthlogin(@RequestBody SocialUserVo vo) throws Exception {
        MemberEntity entity=memberService.Oauthlogin(vo);
        if(entity!=null){
            return R.ok().setData(entity);
        }else {
            return R.error(BizCodeEnum.PHONE_PASSWORD_EXCEPTION.getCode(), BizCodeEnum.PHONE_PASSWORD_EXCEPTION.getMessgae());
        }
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
